﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace ToExcel
{
    public partial class ExportCenter : Form
    {
        public ExportCenter()
        {
            InitializeComponent();
            if (GlobalSetting.Current != null)
            {
                lb_configname.Text = GlobalSetting.Current.Configs["显示名称"];
            }
            else
            {
                btn_Export.Enabled = false;
            }
            lb_total.Text = "";
            lb_dealed.Text = "";
        }
        private void btn_Export_Click(object sender, EventArgs e)
        {
            btn_Export.Enabled = false;
            btn_SelectConfig.Enabled = false;
            lb_total.Text = "";
            lb_dealed.Text = "";
            var t = new Thread(new ParameterizedThreadStart(x => ExportTask()));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }
        private void ExportTask()
        {
            if (GlobalSetting.Current == null) goto NOFILE;
            var file = SysFast.PickFile($"导入来源文件 |*.{GlobalSetting.Current.Configs["数据源文件后缀"]}");
            if (file == string.Empty) goto NOFILE;

            var last_time = DateTime.UtcNow;
            if (GlobalSetting.Current.Configs["数据源格式"] == "raw_log") goto RAWLOG;
            goto ENDExpot;
        RAWLOG:
            var reader = new RawLogReader(file);
            reader.MakeCount(count =>
            {
                if (DateTime.UtcNow - last_time > TimeSpan.FromMilliseconds(100))
                {
                    last_time = DateTime.UtcNow;
                    lb_total.BeginInvoke(new Action(() =>
                    {
                        lb_total.Text = count.ToString();
                    }));
                }
            });
            lb_total.BeginInvoke(new Action(() =>
            {
                lb_total.Text = reader.Count.ToString();
            }));

            GlobalSetting.StartExport();
            var dealed = 0;
            while (reader.HasData)
            {
                reader.ReadNext();
                GlobalSetting.Export(reader.Text);

                dealed++;
                if (DateTime.UtcNow - last_time > TimeSpan.FromMilliseconds(100))
                {
                    last_time = DateTime.UtcNow;
                    lb_dealed.BeginInvoke(new Action(() =>
                    {
                        lb_dealed.Text = dealed.ToString();
                    }));
                }
            }
            lb_dealed.BeginInvoke(new Action(() =>
            {
                lb_dealed.Text = dealed.ToString();
            }));
            GlobalSetting.EndExport();
            goto ENDExpot;

        ENDExpot:
            if (ck_openafter.Checked)
            {
                SysFast.OpenFile(GlobalSetting.OutputPath);
            }
        NOFILE:
            btn_Export.BeginInvoke(new Action(() =>
            {
                btn_Export.Enabled = true;
                btn_SelectConfig.Enabled = true;
            }));
        }

        private void btn_SelectConfig_Click(object sender, EventArgs e)
        {
            btn_Export.Enabled = false;
            btn_SelectConfig.Enabled = false;
            lb_total.Text = "";
            lb_dealed.Text = "";
            var file = SysFast.PickFile($"配置文件 |*.xlsx");
            if (file == string.Empty) goto NOFILE;
            GlobalSetting.LoadSetting(file);

        NOFILE:
            btn_Export.Enabled = GlobalSetting.Current != null;
            btn_SelectConfig.Enabled = true;
        }
    }
}
